import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { ProductStateModel } from '#/api/production/models';
import type { DictModel } from '#/api/system/models';

import { usPassResultOptions } from '#/api/production/models';
import { getDictByCodeApi } from '#/api/system/dict';

const _mapping: Record<string, string> = {};

/**
 * 获取表格查询表单的字段配置。
 */
export function useSearchSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'sn',
      label: '零件码',
    },
    {
      component: 'Input',
      componentProps: {
        allowClear: true,
      },
      fieldName: 'wo',
      label: '工单编号',
    },
  ];
}

/**
 * 获取让步接收编辑表单的字段配置。
 */
export function useConcedeSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      componentProps: {
        disabled: true,
      },
      fieldName: 'sn',
      label: '零件码',
    },
    {
      component: 'RadioGroup',
      componentProps: {
        options: [
          { label: '否', value: false },
          { label: '是', value: true },
        ],
      },
      fieldName: 'isCompleted',
      label: '是否完工',
      defaultValue: false,
      rules: 'required',
    },
  ];
}

/**
 * 获取报废编辑表单的字段配置。
 */
export function useScrapSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      componentProps: {
        disabled: true,
      },
      fieldName: 'sn',
      label: '零件码',
    },
    {
      component: 'ApiSelect',
      componentProps: {
        showSearch: true,
        optionFilterProp: 'label',
        afterFetch: (data: DictModel) => {
          data.items.forEach((item) => {
            _mapping[item.code] = item.label;
          });
          return data.items.map((item) => ({
            label: item.label,
            value: item.code,
          }));
        },
        api: getDictByCodeApi,
        params: { code: 'mes_scrap_category' },
        immediate: false,
      },
      dependencies: {
        trigger(values, form) {
          // 同时设置报废分类名称
          form.setFieldValue(
            'scrapCategoryName',
            _mapping[values.processTypeCode],
          );
        },
        triggerFields: ['scrapCategory'],
      },
      fieldName: 'scrapCategory',
      label: '报废分类',
      rules: 'required',
    },
    {
      component: 'Textarea',
      componentProps: {
        showCount: true,
        maxlength: 128,
      },
      fieldName: 'scrapReason',
      label: '报废原因',
    },
  ];
}

/**
 * 获取表格列配置。
 */
export function useColumns(): VxeTableGridOptions<ProductStateModel>['columns'] {
  return [
    { type: 'seq', width: 60 },
    { field: 'sn', title: '零件码', width: 220 },
    { field: 'wo', title: '工单编号', width: 160 },
    { field: 'productCode', title: '产品编码', width: 140 },
    { field: 'operationCode', title: '当前工序', width: 100 },
    { field: 'workstationCode', title: '当前工位', width: 100 },
    {
      field: 'stage',
      title: '阶段',
      width: 80,
      cellRender: {
        name: 'CellTag',
        options: [
          { color: 'orange', label: '进站', value: 1 },
          { color: 'green', label: '出站', value: 2 },
        ],
      },
    },
    {
      field: 'status',
      title: '状态',
      width: 100,
      cellRender: {
        name: 'CellTag',
        options: usPassResultOptions(),
      },
    },
    {
      field: 'isCompelted',
      title: '是否完工',
      minWidth: 100,
      cellRender: {
        name: 'CellTag',
        options: [
          { color: 'blue', label: '是', value: true },
          { color: 'orange', label: '否', value: false },
        ],
      },
    },
    { field: 'assemblySn', title: '总成码', width: 150 },
    { field: 'inboundTime', title: '进站时间', width: 150 },
    { field: 'outboundTime', title: '出站时间', minWidth: 150 },
    { field: 'unfitCount', title: 'NG 次数', width: 100 },
    {
      field: 'action',
      title: '操作',
      fixed: 'right',
      width: 120,
      slots: { default: 'action' },
    },
  ];
}
